import React from 'react';
import { Form, Icon, Input, Button, Checkbox } from 'antd';
import { connect } from 'dva';
import {POST} from '../../utils/request';

import  './index.less';

const FormItem = Form.Item;

class NormalLoginForm extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            user : {
                userName : 'Caroline'
            }
        }
    }

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                console.log('Received values of form: ', values);
                POST("/api/users").then( res =>{
                    console.log(res)
                }).catch( e=>{
                    console.log("error:"+e)
                })
            }
        });
    }

    render() {
        const { getFieldDecorator } = this.props.form;
        const { user } = this.props;
        return (
            <Form onSubmit={this.handleSubmit} className="login-form">
                <FormItem label="UserName">
                    {getFieldDecorator('userName', {
                        initialValue : user.userName,
                        rules: [{ required: true, message: 'Please input your username!' }],
                    })(
                        <Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="Username" />
                    )}
                </FormItem>
                <FormItem>
                    {getFieldDecorator('password', {
                        rules: [{ required: true, message: 'Please input your Password!' }],
                    })(
                        <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="Password" />
                    )}
                </FormItem>
                <FormItem>
                    {getFieldDecorator('remember', {
                        valuePropName: 'checked',
                        initialValue: true,
                    })(
                        <Checkbox>Remember me</Checkbox>
                    )}
                    <a className="login-form-forgot" href="">Forgot password</a>
                    <Button type="primary" htmlType="submit" className="login-form-button"> Log in </Button>
                    Or <a href="">register now!</a>
                </FormItem>
            </Form>
        );
    }
}

const WrappedNormalLoginForm = Form.create()(NormalLoginForm);

// export default Products;
export default connect(({ user, products }) => ({
    user,
}))(WrappedNormalLoginForm);